﻿using assignment6.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace assignment6
{
    public partial class Form1 : Form
    {
        //三个表格分别为0.1.2
        private int tableIndex = 0;
        //三种操作
        private int operateType;
        private string queryCondition;
        public Form1()
        {
            InitializeComponent();
            try
            {
                DataSet ds = new DataSet();

                ds = SqlService.query(tableIndex, queryCondition, "");

                if (ds.Tables.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                ds = SqlService.query(tableIndex, queryCondition, textBox1.Text);
                if (ds.Tables.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //新增
        private void add_Click(object sender, EventArgs e)
        {
            //弹窗进行新增
            Form2 form2 = new Form2(tableIndex, dataGridView1.Columns);
            form2.ShowDialog();
        }

        //删除
        private void delete_Click(object sender, EventArgs e)
        {
            try
            {
                //确认是否选中一行数据
                if (dataGridView1.SelectedRows.Count < 0)
                {
                    MessageBox.Show("未选择数据！");
                    return;
                }
                int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Id"].Value);
                SqlService.delete(id, tableIndex);
                //confirmButton.Enabled = false;
                MessageBox.Show("操作成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //编辑
        private void edit_Click(object sender, EventArgs e)
        {
            try
            {
                //先判断是否选中一行数据
                if (dataGridView1.SelectedRows.Count <= 0)
                {
                    MessageBox.Show("未选择数据！");
                    return;
                }
                //弹出inputform窗口
                Form2 form2 = new Form2(tableIndex, dataGridView1.Columns, dataGridView1.SelectedRows[0].Cells);
                form2.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //按照学校名称查询
        private void SchoolName_Click(object sender, EventArgs e)
        {
            tableIndex = 0;
            queryCondition = "Name";
            query.Text = SchoolName.Text;
            edit.Enabled = true;
            delete.Enabled = true;
            add.Enabled = true;
        }

        //按照班级名称查询
        private void ClassName_Click(object sender, EventArgs e)
        {
            tableIndex = 1;
            queryCondition = "Name";
            query.Text = ClassName.Text;
            edit.Enabled = true;
            delete.Enabled = true;
            add.Enabled = true;
        }

        //按照学生名称查询
        private void StudentName_Click(object sender, EventArgs e)
        {
            tableIndex = 2;
            queryCondition = "Name";
            query.Text = StudentName.Text;
            edit.Enabled = true;
            delete.Enabled = true;
            add.Enabled = true;
        }

        //按照操作类型查询日志
        private void Type_Click(object sender, EventArgs e)
        {
            tableIndex = -1;
            queryCondition = "OperateType";
            query.Text = Type.Text;
            edit.Enabled = false;
            delete.Enabled = false;
            add.Enabled = false;
        }

        //按照操作对应表格查询日志
        private void Table_Click(object sender, EventArgs e)
        {
            tableIndex = -1;
            queryCondition = "OperateDataType";
            query.Text = Table.Text;
            edit.Enabled = false;
            delete.Enabled = false;
            add.Enabled = false;
        }
    }
}
